<?php
declare (strict_types=1);

namespace app\user\service\teacher;

use app\common\service\BaseService;
use app\user\logic\teacher\Teacher as TeacherLogic;
use think\facade\Event;
use think\facade\Request;

/**
 * 讲师接口类
 */
class Teacher extends BaseService
{

    /**
     * 讲师列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-16
     *
     * @return   [type]                         [description]
     */
    public function index()
    {
        // 搜索功能
        // Request::has('title', 'get', true) && $map['title'] = Request::get('title/s');

        $map = [];

        $mhm_id = request()->mhm_id;
        if (input('?get.mhm_id') && input('get.mhm_id/d')) $mhm_id = input('get.mhm_id/d');
        $map[] = ['__TABLE__.mhm_id', '=', $mhm_id];
        // 讲师分类
        if (input('?get.category') && input('get.category/d')) $map[] = ['category', 'like', '%,' . input('get.category/d') . ',%'];
        // 讲师名称
        if (input('?get.title') && input('get.title/s')) {
            $map[] = ['teacherName.nick_name', 'like', '%' . input('get.title/s') . '%'];
        }
        // 讲师等级
        if (input('?get.level_id') && input('get.level_id/d')) $map[] = ['level_id', '=', input('get.level_id/d')];

        $data = app(TeacherLogic::class)->getTeacherList($map);
        // $data->append(['status_text', 'user', 'school']);
        return $this->parseToData($data, 1, '获取成功');
    }

    // 讲师主页基础信息
    public function basic(int $id)
    {
        $data = app(TeacherLogic::class)->getBasicInfo($id);
        Event::trigger('WebsiteVisit', [
            'mhm_id'        => $data['mhm_id'],
            'resource_type' => 'teacher',
            'resource_id'   => $id,
        ]);
        return $this->parseToData($data, 1, '获取成功');
    }

    // 讲师简介
    public function info(int $id)
    {
        $data = app(TeacherLogic::class)->getInfo($id);
        return $this->parseToData($data, 1, '获取成功');
    }

    // 讲师主页课程
    public function course(int $id)
    {
        $param = Request::get();
        $data  = app(TeacherLogic::class)->getCourse($id, $param);
        return $this->parseToData($data, 1, '获取成功');
    }

    // 讲师认证页面
    public function authFetch()
    {
        $data = app(TeacherLogic::class)->authFetch();
        return $this->parseToData($data, 1, '获取成功');
    }

    // 提交讲师认证
    public function authSubmit()
    {
        $param = Request::only([
            'category',
            'IDcard',
            'IDcard_positive',
            'IDcard_side',
            'certification',
        ], 'post');

        $data = app(TeacherLogic::class)->authSubmit($param);
        return $this->parseToData($data, 1, '申请成功，请等待审核');
    }

    /**
     * 讲师等级信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2022-09-30
     */
    public function levelList()
    {
        $mhm_id = (Request::has('mhm_id', 'get', true) && Request::get('mhm_id/d')) ? Request::get('mhm_id/d') : request()->mhm_id;
        $cate   = app(TeacherLogic::class)->getSelectLevel([['mhm_id', '=', $mhm_id]]);

        return $this->parseToData($cate, 1, '获取成功');
    }

}
